﻿using System;
using System.Collections.Generic;
using System.Collections.ObjectModel;
using System.IO;
using System.Linq;
using System.Net;
using System.Text;
using System.Threading.Tasks;

namespace Server.CustomObjects
{
    public static class Log
    {
        static StreamWriter SW_LogFile;

        public enum eLogType { Server, DataBase };
        public enum eDirection { Receiving, Sending };        
        public delegate void MessageLogEventHandler(String pTimeStamp, String pDirection, String pMessageType, String pSourceID, String pTargetID, String pContent);
        public delegate void CustomLogEventHandler(String pTimeStamp, String pType, String pContent);
        public static event MessageLogEventHandler NewMessage;
        public static event CustomLogEventHandler NewCustomLog;

        public static void Write(eLogType pLogType, String pContent)
        {
            WriteToLogFile(DateTime.Now.ToString() + " ==> " + Enum.GetName(typeof(eLogType), pLogType) + "  " + pContent);

            if (NewCustomLog != null)
                NewCustomLog(DateTime.Now.ToString(), Enum.GetName(typeof(eLogType), pLogType),pContent);
        }

        public static void Write(eDirection pDirection, Message pMessage)
        {
            WriteToLogFile(DateTime.Now.ToString() + " ==> " + Enum.GetName(typeof(eDirection), pDirection) + "  " +
                            Enum.GetName(typeof(Message.eCommand), pMessage.Command) + "  " +
                            pMessage.SourceID.ToString() + "  " + pMessage.TargetID.ToString() + "  " + pMessage.Content);

            if (NewMessage != null)
                NewMessage( DateTime.Now.ToString(), Enum.GetName(typeof(eDirection), pDirection), 
                            Enum.GetName(typeof(Message.eCommand), pMessage.Command), 
                            pMessage.SourceID.ToString(), pMessage.TargetID.ToString(), pMessage.Content);
        }

        static void WriteToLogFile(String pLogLine)
        {
            if (!File.Exists("MeetUp_Log.txt"))
                SW_LogFile = new StreamWriter("MeetUp_Log.txt");
            else
                SW_LogFile = File.AppendText("MeetUp_Log.txt");

            SW_LogFile.WriteLine(pLogLine);
            SW_LogFile.Close();
        }
    }
}
